<?php
namespace app\common\model;
use think\Db;
use think\Model;

class Address extends Model{

    protected $table = "jiujiu_user_address";

    public function  getUserAddress($uid){

        return Db::table('jiujiu_user_address')
            ->field(['jiujiu_user_address.*',
                'country.cityname as country',
                'city.cityname as city',
                'district.cityname as district'])
            ->join('jiujiu_city country','country.id=jiujiu_user_address.country','LEFT')
            ->join('jiujiu_city city','city.id=jiujiu_user_address.city','LEFT')
            ->join('jiujiu_city district','district.id=jiujiu_user_address.district','LEFT')
            ->where('jiujiu_user_address.uid',$uid)
            ->select();
    }



    public function  getAddressById($id){

        return Db::table('jiujiu_user_address')
            ->field(['jiujiu_user_address.*',
                'country.cityname as country',
                'city.cityname as city',
                'district.cityname as district'])
            ->join('jiujiu_city country','country.id=jiujiu_user_address.country','LEFT')
            ->join('jiujiu_city city','city.id=jiujiu_user_address.city','LEFT')
            ->join('jiujiu_city district','district.id=jiujiu_user_address.district','LEFT')
            ->where('jiujiu_user_address.id',$id)
            ->find();
    }



    public function  getUserDefaultAddress($uid){

        return Db::table('jiujiu_user_address')
            ->field(['jiujiu_user_address.*',
                'country.cityname as country',
                'city.cityname as city',
                'district.cityname as district'])
            ->join('jiujiu_city country','country.id=jiujiu_user_address.country','LEFT')
            ->join('jiujiu_city city','city.id=jiujiu_user_address.city','LEFT')
            ->join('jiujiu_city district','district.id=jiujiu_user_address.district','LEFT')
            ->where('jiujiu_user_address.uid',$uid)
            ->where('jiujiu_user_address.isDefault',1)
            ->find();
    }


    public function  getAUserAddress($uid){

        return Db::table('jiujiu_user_address')
            ->field(['jiujiu_user_address.*',
                'country.cityname as country',
                'city.cityname as city',
                'district.cityname as district'])
            ->join('jiujiu_city country','country.id=jiujiu_user_address.country','LEFT')
            ->join('jiujiu_city city','city.id=jiujiu_user_address.city','LEFT')
            ->join('jiujiu_city district','district.id=jiujiu_user_address.district','LEFT')
            ->where('jiujiu_user_address.uid',$uid)
            ->find();
    }

}